﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace _1042009_1042034
{
    public partial class uctPhanTrang : UserControl
    {
        private int m_PageIndex = 1;
        private int m_Page = 1;
        public uctPhanTrang()
        {
            InitializeComponent();
        }
        private Color DefaultColor;
       
        private void Reload()
        {
            this.Controls.Clear();
            for (int i = 1; i <= this.m_Page; i++)
            {
                Button btn = new Button();
                this.DefaultColor = btn.BackColor;
                btn.Text = i.ToString();
                btn.Location = new Point(i * 40, 0);
                btn.Size = new Size(35, 35);
                btn.Click += new EventHandler(btn_Click);
                this.Controls.Add(btn);
            }
        }
        public delegate void PageClick(int Page);
        public event PageClick OnPageClick = null;

        // Lam dep o day.
        void UpdatePageIndex(int iPage)
        {
            this.Controls[this.m_PageIndex - 1 ].BackColor = this.DefaultColor;
            this.Controls[this.m_PageIndex - 1].Enabled = true;
            this.Controls[iPage -1].Enabled = false;
            this.Controls[iPage -1].BackColor = Color.Red;
            this.m_PageIndex = iPage;
        }
        void btn_Click(object sender, EventArgs e)
        {
            Button x = (Button)sender;
            int NewPage = int.Parse(x.Text);
            this.UpdatePageIndex(NewPage);
            if (OnPageClick != null)
            {
                OnPageClick(NewPage);
            }
        }
             
        public void SetValue(int TongSoBoDuLieu, int SoBoTrong1Trang, int iPageIndex = 1)
        {
            this.m_Page = TongSoBoDuLieu / SoBoTrong1Trang;
            if (TongSoBoDuLieu % SoBoTrong1Trang > 0)
            {
                this.m_Page++;
            }
            if (this.m_Page == 0)
            { 
                this.m_Page++;                
            }

            this.m_PageIndex = iPageIndex;
            if (iPageIndex >= this.m_Page)
            {
                this.m_PageIndex = this.m_Page;
            }
            this.Reload();
            this.UpdatePageIndex(this.m_PageIndex);
        }
        public int GetPageIndex()
        {
            return this.m_PageIndex;
        }
        public int GetTotalPage()
        {
            return this.m_Page;
        }
        private void uctPhanTrang_Load(object sender, EventArgs e)
        {
            this.Reload();
        }
    }
}
